<?php

namespace app\api\controller\audience;

use app\api\controller\common\ApiBase;
use think\Db; 
use think\Controller; // dengh quxiao

use function PHPSTORM_META\type;

class Whiteblacklist extends ApiBase
{
    public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        $tabName="";
        if($result){
            $tabName=$result['create_tablename'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
     }

    /**
     * 列表
     *
     * @return void
     */
    public function index()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $page = isset($post['page']) && $post['page'] != '' ? $post['page'] : 1;
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $pageSize = isset($post['pageSize']) && $post['pageSize'] != '' ? $post['pageSize'] : 10;
            $keyWord=isset($post['keyWord']) && $post['keyWord'] != '' ? $post['keyWord'] : '';
            $type=isset($post['type']) && $post['type'] != '' ? $post['type'] : 1;
            $where['is_delete']=0;
            $where['type']=$type;
            if($keyWord){
                $where['title']=array('like',"'%$keyWord%'");
            }
            $where['exhibition_id']=$exhibition_id;
            $list = db('white_black_list')
            ->where($where)
            ->order('id desc')
            ->limit(($page-1)*$pageSize.','.$pageSize)
            ->select();
            $total = db('white_black_list')
            ->where($where)
            ->count();
            return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total]]);
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 新增
     *
     * @return void
     */
    public function add()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $data['exhibition_id'] =isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : '';
            if(!$data['exhibition_id']){
                return json(['code'=>0,'msg'=>'展会数据有误']);
            }
            $data['type'] =isset($post['type']) && $post['type'] != '' ? $post['type'] : 1;
            $data['title'] =isset($post['title']) && $post['title'] != '' ? $post['title'] : '';
            $wRes=db('white_black_list')->where(['title'=>$data['title'],'is_delete'=>0,'type'=>$data['type'],'exhibition_id'=>$data['exhibition_id']])->find();
            if($wRes){
                return json(array('code' => '0', 'msg' => '分类名称重复'));
            }
            $data['create_time'] =time();
            $data['update_time'] =time();
            $res=db('white_black_list')->insert($data);
            if(!$res){
                return json(array('code' => '0', 'msg' => '操作失败'));
            }
            return json(array('code' => '1', 'msg' => '操作成功'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 编辑
     *
     * @return void
     */

    public function edit()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $id =isset($post['id']) && $post['id'] != '' ? $post['id'] : '';
            $wRes=db('white_black_list')->where(['id'=>$id,'is_delete'=>0])->find();
            if(!$wRes){
                return json(array('code' => '0', 'msg' => '名单数据错误'));
            }
            $data['exhibition_id'] =isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : '';
            if(!$data['exhibition_id']){
                return json(['code'=>0,'msg'=>'展会数据有误']);
            }
            $data['type'] =isset($post['type']) && $post['type'] != '' ? $post['type'] : 1;
            $data['title'] =isset($post['title']) && $post['title'] != '' ? $post['title'] : '';
            $wTitle=db('white_black_list')->where(['title'=>$data['title'],'is_delete'=>0,'type'=>$data['type'],'exhibition_id'=>$data['exhibition_id']])->find();
            if($wTitle){
                return json(array('code' => '0', 'msg' => '分类名称重复'));
            }
            $data['update_time'] =time();
            $res=db('white_black_list')->where(['id'=>$id])->update($data);
            if(!$res){
                return json(array('code' => '0', 'msg' => '操作失败'));
            }
            return json(array('code' => '1', 'msg' => '操作成功'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


     /**
     * 删除
     *
     * @return void
     */

    public function del()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $id =isset($post['id']) && $post['id'] != '' ? $post['id'] : '';
            if(!$id){
                return json(array('code' => '0', 'msg' => '名单数据错误'));
            }
            $where['id']=['in',$id];
            $data['is_delete']=1;
            $res=db('white_black_list')->where($where)->update($data);
            if(!$res){
                return json(['code'=>0,'msg'=>'操作失败，请重试']);
            }
            return json(array('code' => '1', 'msg' => '操作成功'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    /**
     * 删除
     *
     * @return void
     */

    public function open()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $is_open =isset($post['is_open']) && $post['is_open'] != '' ? $post['is_open'] : 0;
            $exhibition_id =isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : '';
            if(!$exhibition_id){
                return json(['code'=>0,'msg'=>'展会数据有误']);
            }
            $res=db('exhibition')->where(['id'=>$exhibition_id])->update(['is_open'=>$is_open]);
            if(!$res){
                return json(['code'=>0,'msg'=>'操作失败，请重试']);
            }
            return json(array('code' => '1', 'msg' => '操作成功'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    /**
     * 删除
     *
     * @return void
     */

    public function getOpen()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id =isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : '';
            if(!$exhibition_id){
                return json(['code'=>0,'msg'=>'展会数据有误']);
            }
            $res=db('exhibition')->where(['id'=>$exhibition_id,'is_delete'=>0])->find();
            return json(array('code' => '1', 'msg' => '操作成功','data'=>$res));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


}
